﻿//
// © Algobox Ltd - Benn Eichhorn
//
using System.Collections.Generic;
using Algobox.Objects.Finance;
using Algobox.Objects.Finance.Assets;
using Algobox.Objects.Finance.Assets.Stock;
using System.Collections.Concurrent;

namespace Algobox.Data.Finance
{
    public class SterlingSpikeTestData : Algobox.Structure.Finance.Strategies.SpikeArb.ISpikeArbData
    {
        public SterlingSpikeTestData(ConcurrentDictionary<string, StockSecurity> stocks)
        {
            _stocks = stocks;
        }

        ConcurrentDictionary<string, StockSecurity> _stocks;
        public IDictionary<string, StockSecurity> Stocks { get { return _stocks; } }

        public string FeedName
        {
            get { return "SterlingTestData"; }
        }        

        public bool TryLoadStrategySpikeArb(Structure.Finance.Strategies.SpikeArb.SpikeArbManagement spikeArb)
        {
            // try and load existing stock or create it if not found
            StockSecurity stock = _stocks.AddOrUpdate("C",
                (inSymbol) =>
                {
                    return new StockSecurity("C", Markets.MarketMIC.XNYS)
                    {
                        Currency = Currencies.Currency.USD,
                        Index = "DJI",
                        Sector = "Technology",
                        IndustryGroup = "Computers",
                        Industry = "Hardware",
                        ISIN = "123456789012"
                    };
                },
                (inSymbol, inStock) =>
                {
                    stock = inStock;
                    return inStock;
                });



            spikeArb.AddOrUpdate( // Partial fill (Half)
                0, stock, true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            /*
            spikeArb.AddOrUpdate( // Partial fill (Half)
                1, "AAPL", Markets.MarketMIC.XNYS, Currencies.Currency.USD, "DJI", "Technology", "Computers", "Hardware", "123456789012", true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            spikeArb.AddOrUpdate( // Stays in sending mode (New or replace)
                2, "CIEN", Markets.MarketMIC.XNYS, Currencies.Currency.USD, "DJI", "Technology", "Comunications", "Equipment", "123456789012", true, true, true,
                true, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            spikeArb.AddOrUpdate( // Stays in cancel pending mode and does not fill
                3, "CP", Markets.MarketMIC.XNYS, Currencies.Currency.USD, "DJI", "Industrials", "Transport", "Freight", "123456789012", true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            spikeArb.AddOrUpdate( // Rejected Order
                4, "GE", Markets.MarketMIC.XNYS, Currencies.Currency.USD, "DJI", "Industrials", "Conglomerates", "Everything", "123456789012", true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            spikeArb.AddOrUpdate( // Does not fill
                5, "JNJ", Markets.MarketMIC.XNYS, Currencies.Currency.USD, "DJI", "Healthcare", "Pharma", "Diversified", "123456789012", true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            spikeArb.AddOrUpdate( // Too late to cancel
                6, "KO", Markets.MarketMIC.XNYS, Currencies.Currency.USD, "DJI", "Consumer", "Non-Cyclical", "Beverages", "123456789012", true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            spikeArb.AddOrUpdate( // does not fill
                7, "MSFT", Markets.MarketMIC.XNYS, Currencies.Currency.USD, "DJI", "Tech", "Software", "Software", "123456789012", true, true, true,
                false, 10000m, 1m, 0.1m, false, 10000m, 2m, 0.2m, false, 10000m, 3m, 0.4m, false, 10000m, 4m, 0.5m
                );
            */

            foreach (StockSecurity security in Stocks.Values)
            {
                security.Listings.TryAdd(Exchanges.ExchangeMIC.XNAS, new StockListing(security, Exchanges.ExchangeMIC.XNAS, TickStatic.Default(), 3));
                security.Listings.TryAdd(Exchanges.ExchangeMIC.BATS, new StockListing(security, Exchanges.ExchangeMIC.BATS, TickStatic.Default(), 1));
                security.Listings.TryAdd(Exchanges.ExchangeMIC.ARCX, new StockListing(security, Exchanges.ExchangeMIC.ARCX, TickStatic.Default(), 2));
            }

            return true;
        }

        public bool TrySaveStrategySpikeArb(Structure.Finance.Strategies.SpikeArb.SpikeArbManagement spikeArb)
        {
            return true;
        }        
    }
}
